package com.easy.frame.servers.sys.process.getter;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.easy.frame.base.basic.exception.ServiceException;
import com.easy.frame.common.process.core.emum.ExaminePersonType;
import com.easy.frame.common.process.core.getter.AbstractExaminePersonGetter;
import com.easy.frame.servers.sys.entity.SysStaff;
import com.easy.frame.servers.sys.mapper.staff.SysStaffMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 上级id获取类
 *
 * @author zzf
 * @date 2021/3/19 15:45
 */
@Component
public class SuperiorGetter extends AbstractExaminePersonGetter {

    @Resource
    private SysStaffMapper staffMapper;

    protected SuperiorGetter() {
        super(ExaminePersonType.INITIATOR);
    }

    @Override
    public Collection<Long> getExaminePerson(Object objectParam) {
        SysStaff param;
        if (objectParam instanceof SysStaff) {
            param = (SysStaff) objectParam;
        } else {
            return null;
        }
        List<SysStaff> staffList = staffMapper.selectList(new LambdaQueryWrapper<SysStaff>()
                .eq(SysStaff::getTenantId, param.getTenantId())
        );
        if (CollectionUtil.isEmpty(staffList)) {
            throw new ServiceException("流程执行失败，未找到用户[{}](id: {})的上级！", param.getName(), param.getId().toString());
        }
        return staffList.stream().map(SysStaff::getId).collect(Collectors.toList());
    }
}
